﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace DAL.Gateway
{
    public class MSAccess : GDatos
    {
        static readonly System.Collections.Hashtable ColComandos = new System.Collections.Hashtable();
        public override string CadenaConexion
        {
            get
            {
                if (MCadenaConexion.Length == 0)
                {
                    if (MBase.Length != 0 && MServidor.Length != 0)
                    {
                        var sCadena = new System.Text.StringBuilder("");
                        sCadena.Append("Provider=Microsoft.Jet.OLEDB.4.0;");
                        sCadena.Append("Data Source=<SERVIDOR>;");
                        sCadena.Append("User =<USER>;");
                        sCadena.Append("Password=<PASSWORD>;");
                        sCadena.Replace("<SERVIDOR>", Servidor);
                        sCadena.Replace("<USER>", Usuario);
                        sCadena.Replace("<PASSWORD>", Password);

                        return sCadena.ToString();
                    }
                    throw new Exception("No se puede establecer la cadena de conexión en la clase MsAccess");
                }
                return null;
            }// end get
            set
            { MCadenaConexion = value; } // end set
        }

        protected override System.Data.IDbConnection CrearConexion(string cadena)
        {
            return new System.Data.OleDb.OleDbConnection(cadena);
        }

        protected override System.Data.IDbCommand Comando(string procedimientoAlmacenado)
        {
            OleDbCommand com;
            if (ColComandos.Contains(procedimientoAlmacenado))
                com = (OleDbCommand)ColComandos[procedimientoAlmacenado];
            else
            {
                var con2 = new OleDbConnection(CadenaConexion);
                con2.Open();

                com = new OleDbCommand(procedimientoAlmacenado, con2)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };
                OleDbCommandBuilder.DeriveParameters(com);
                con2.Close();
                con2.Dispose();
                ColComandos.Add(procedimientoAlmacenado, com);
            }
            com.Connection = (OleDbConnection)Conexion;
            com.Transaction = (OleDbTransaction)MTransaccion;
            return com;
        }

        protected override System.Data.IDbCommand ComandoSql(string comandoSql)
        {
            return new OleDbCommand(comandoSql, (OleDbConnection)Conexion, (OleDbTransaction)MTransaccion);
        }

        protected override System.Data.IDataAdapter CrearDataAdapter(string procedimientoAlmacenado, params object[] args)
        {
            var da = new OleDbDataAdapter((OleDbCommand)Comando(procedimientoAlmacenado));
            if (args.Length != 0)
                CargarParametros(da.SelectCommand, args);
            return da;
        }

        protected override System.Data.IDataAdapter CrearDataAdapterSql(string comandoSql)
        {
            var da = new OleDbDataAdapter((OleDbCommand)ComandoSql(comandoSql));
            return da;
        }

        protected override void CargarParametros(System.Data.IDbCommand com, object[] args)
        {
            for (var i = 0; i < com.Parameters.Count; i++)
            {
                var p = (OleDbParameter)com.Parameters[i];
                p.Value = i <= args.Length ? args[i] : null;
            }
        }
        public MSAccess(string servidor, string @base, string usuario, string password)
        {
            Base = @base;
            Servidor = servidor;
            Usuario = usuario;
            Password = password;
        }
        public MSAccess(string CadenaDeConexion)
        {
            this.CadenaConexion = CadenaDeConexion;
        }
    }
}
